# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        a=0
        head1=head
        head2=head
        while head1:
            a+=1
            head1=head1.next
        a-=(n-1)
        b=0
        while b<a:
            b+=1
            head2=head2.next
        head2=head2.next.next
        return head

